package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDao {
    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
//        拼接sql语句
        String sql = "select * from user where username='" + userModel.getUsername() + "' and password='" + userModel.getPassword() + "'";
//        通过调用工具类JDBC查询数据库，得到结果集
        ResultSet resultSet = JDBC.query(sql);
//        创建一个空值对象
        UserModel model = null;
        try {
//            如果结果集里面有值，则进入while循环
            while (resultSet.next()) {
                model = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
//        1.拼接sql语句
        String sql = " select * from user";
        sql += setSql(userModel);
        sql += " order by id desc ";
//        pageNo第几页，pageSize每一页显示的条数
        sql += " limit " + (userModel.getPage() - 1) * +userModel.getLimit() + ", " + userModel.getLimit() + ";";
        List<UserModel> list = new ArrayList<>();
//        集合和数组的区别？
        //       2. 通过调用工具类JDBC查询数据库，得到结果集
        ResultSet resultSet = JDBC.query(sql);
//        3.解析结果集
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setSex(resultSet.getString("sex"));
                model.setBirthday(resultSet.getString("birthday"));
                model.setHobby(resultSet.getString("hobby"));
                model.setEnable(resultSet.getInt("enable"));
                model.setAddress(resultSet.getString("address"));
                model.setEmail(resultSet.getString("email"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String setSql(UserModel userModel) {
        StringBuilder sql = new StringBuilder();
        sql.append(" where 1=1 ");
        if (!"".equals(userModel.getUsername())) {
            sql.append(" and username like  '%").append(userModel.getUsername()).append("%'  ");
        }
        if (!"".equals(userModel.getSex())) {
            sql.append("and sex='").append(userModel.getSex()).append("' ");
        }
        if (!"".equals(userModel.getEmail())) {
            sql.append("and email like '%").append(userModel.getEmail()).append("%' ");
        }

//        生日由几种情况
//        1.开始生日为空，结束生日不为空
        if (!"".equals(userModel.getStart()) && "".equals(userModel.getEnd())) {
            sql.append(" and birthday >='").append(userModel.getStart()).append("'");
        }
//        2.开始生日不为空，结束生日为空
        if ("".equals(userModel.getStart()) && !"".equals(userModel.getEnd())) {
            sql.append(" and birthday <='").append(userModel.getEnd()).append("'");
        }
//        2.开始生日不为空，结束生日不为空

        if (!"".equals(userModel.getStart()) && !"".equals(userModel.getEnd())) {
            sql.append(" and birthday between '").append(userModel.getStart()).append("' and '").append(userModel.getEnd()).append("'");
        }
        if (userModel.getEnable() != -1) {
            sql.append(" and enable=").append(userModel.getEnable());
        }

        if (!"".equals(userModel.getHobby())) {
            String[] hobbys = userModel.getHobby().split(",");
            for (int i = 0; i < hobbys.length; i++) {
                sql.append(" and find_in_set('").append(hobbys[i]).append("',hobby)");
            }

        }
        return sql.toString();
    }

    @Override
    public int getCount(UserModel userModel) {
        String sql = "select count(*) count from user";
        sql += setSql(userModel);
        ResultSet resultSet = JDBC.query(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updateEnable(UserModel userModel) {
        String sql = " UPDATE `user` SET `enable` = '" + userModel.getEnable() + "' WHERE (`id` = '" + userModel.getId() + "');";
//        int update = JDBC.update(sql);
        return JDBC.update(sql);
    }

    @Override
    public int updateField(UserModel userModel) {
        String sql = " UPDATE `user` " +
                "SET `" + userModel.getField() + "` = '" + userModel.getValue() + "' " +
                "WHERE (`id` = '" + userModel.getId() + "');";
//        int update = JDBC.update(sql);
        return JDBC.update(sql);
    }

    @Override
    public UserModel findByUsername(UserModel userModel) {
        String sql = "select * from user where username ='" + userModel.getValue() + "'";
        ResultSet resultSet = JDBC.query(sql);
        UserModel model = new UserModel();
//        3.解析结果集
        try {
            while (resultSet.next()) {

                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setSex(resultSet.getString("sex"));
                model.setBirthday(resultSet.getString("birthday"));
                model.setHobby(resultSet.getString("hobby"));
                model.setEnable(resultSet.getInt("enable"));
                model.setAddress(resultSet.getString("address"));
                model.setEmail(resultSet.getString("email"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int delete(UserModel userModel) {
        String sql = "delete from user where id=" + userModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int add(UserModel userModel) {
        String sql = "INSERT INTO `user` (" +
                "`username`, `address`, `create_time`, " +
                "`email`, `enable`, `password`, " +
                "`phone`, `sex`, `update_time`, " +
                "`hobby`, `birthday`, `remark`) VALUES (" +
                "'" + userModel.getUsername() + "', '', now()," +
                " '" + userModel.getEmail() + "', '" + userModel.getEnable() + "', '" + userModel.getPassword() + "'," +
                " '" + userModel.getPhone() + "', '" + userModel.getSex() + "', now()," +
                " '" + userModel.getHobby() + "', '" + userModel.getBirthday() + "', '');\n";
        return JDBC.update(sql);
    }

    @Override
    public UserModel findById(UserModel userModel) {
        String sql = "select * from user where id=" + userModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        UserModel model = new UserModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setSex(resultSet.getString("sex"));
                model.setBirthday(resultSet.getString("birthday"));
                model.setHobby(resultSet.getString("hobby"));
                model.setEnable(resultSet.getInt("enable"));
                model.setAddress(resultSet.getString("address"));
                model.setEmail(resultSet.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "UPDATE `user` SET " +
                "`username` = '" + userModel.getUsername() + "', ";
        if (userModel.getAddress() != null) {
            sql += "`address` = '" + userModel.getAddress() + "', ";
        }
        if (userModel.getEmail() != null) {
            sql += "`email` = '" + userModel.getEmail() + "', ";
        }
        if (userModel.getEnable() != -1) {
            sql += " `enable` = '" + userModel.getEnable() + "',";
        }

        sql += "`phone` = '" + userModel.getPhone() + "', " +
                "`sex` = '" + userModel.getSex() + "', " +
                "`update_time` = now()," +
                " `hobby` = '" + userModel.getHobby() + "', " +
                "`birthday` = '" + userModel.getBirthday() + "'," +
                " `remark` = '' " +
                "WHERE (`id` = '" + userModel.getId() + "');";
        return JDBC.update(sql);
    }
}
